package com.lzc.springcloud.user.application.secruity;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;

public class CustomAuthorizationServerTokenServices implements AuthorizationServerTokenServices, ConsumerTokenServices {

    private TokenStore tokenStore;

    @Override
    public OAuth2AccessToken createAccessToken(OAuth2Authentication oAuth2Authentication) throws AuthenticationException {
        OAuth2AccessToken orgToken = tokenStore.getAccessToken(oAuth2Authentication);
        OAuth2RefreshToken refreshToken;
        if (orgToken != null) {
            if (orgToken.getRefreshToken() != null) {
                refreshToken = orgToken.getRefreshToken();
                tokenStore.removeRefreshToken(refreshToken);
            }
            tokenStore.removeAccessToken(orgToken);
        }
        return null;
    }

    @Override
    public OAuth2AccessToken refreshAccessToken(String s, TokenRequest tokenRequest) throws AuthenticationException {
        return null;
    }

    @Override
    public OAuth2AccessToken getAccessToken(OAuth2Authentication oAuth2Authentication) {
        return null;
    }

    @Override
    public boolean revokeToken(String s) {
        return false;
    }
}
